﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Resources;
using Cesave.Domain.PermisosModule.Contracts.Municipios;
using Cesave.Domain.PermisosModule.Entities;
using Cesave.Infrastructure.CrossCutting.IoC;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Juntas
{
	public partial class JuntasViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<Municipio> _municipios;

		/// <summary>
		/// 
		/// </summary>
		private Municipio _municipio;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<Municipio> Municipios
		{
			get
			{
				return _municipios;
			}
			private set
			{
				_municipios = value;
				if (_municipios != null)
					RaisePropertyChanged("Municipios");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Municipio Municipio
		{
			get
			{
				return _municipio;
			}
			private set
			{
				_municipio = value;
				if (_municipio != null)
					RaisePropertyChanged("Municipio");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsMunicipioReady
		{
			get
			{
				return Municipio != null;
			}
		}

		#endregion

		#region Actions

		/// <summary>
		/// 
		/// </summary>
		private void LoadMunicipios()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					IMunicipioService municipioService = IoCFactory.Resolve<IMunicipioService>();
					e.Result = municipioService.FindMunicipiosByEstado(Estado.EstadoGuid);
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						Municipios = e.Result as List<Municipio>;
					else if (e.Error != null)
						MessageBox.Show(
							_juntasView,
							e.Error.Message,
							WindowTitles.JuntasWindowTitle,
							MessageBoxButtons.OK,
							MessageBoxIcon.Error
							);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		private void OnMunicipiosPropertyChanged()
		{
			_juntasView.municipiosBindingSource.DataSource = Municipios;
			_juntasView.municipiosBindingSource.CurrentChanged += SetCurrentMunicipio;
			SetCurrentMunicipio(_juntasView.municipiosBindingSource, new EventArgs());
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentMunicipio(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if (bindingSource != null)
				if (bindingSource.Current != null)
					Municipio = bindingSource.Current as Municipio;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnMunicipioPropertyChanged()
		{
			
		}

		#endregion

	}
}
